package org.example.bigdata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.jsonwebtoken.Claims;
import org.example.bigdata.dto.LoginFormDto;
import org.example.bigdata.dto.UserDto;
import org.example.bigdata.entity.User;
import org.example.bigdata.service.UserService;
import org.example.bigdata.mapper.UserMapper;
import org.example.bigdata.utils.JwtUtils;
import org.example.bigdata.utils.RegexUtils;
import org.example.bigdata.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* @author Jmx
* @description 针对表【user】的数据库操作Service实现
* @createDate 2024-04-29 10:55:10
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result register(User user) {
        // 1.验证手机号码是否是正确格式
        if(RegexUtils.isPhoneInvalid(user.getPhone())){
            // 2.如果不符合，返回错误信息
            return Result.fail("手机号格式错误！");
        }
        // 2.在数据库中创建用户
        // 2.1 查询该手机号是否注册过
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getPhone, user.getPhone());
        boolean exists = exists(lambdaQueryWrapper);
        if(exists){
            // 2.2 如果注册过，返回报错
            return Result.fail("该手机号已经注册过！");
        }
        // 2.3 未注册，在数据库中创建用户
        save(user);
        return Result.success();
    }

    @Override
    public Result login(LoginFormDto loginFormDto) {
        // 1.校验用户是否存在
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getPhone, loginFormDto.getPhone());
        User one = getOne(lambdaQueryWrapper);
        if(one==null){
            return Result.fail("用户不存在！");
        }

        if (!Objects.equals(loginFormDto.getPassword(), one.getPassword())) {
            return Result.fail("密码错误！");
        }
        // 2，登录成功，返回token
        Map<String, Object> claims = new HashMap<>();
        //此处要大写，在JwtUtils工具中会自动首字符大写
        claims.put("phone", one.getPhone());

        String jwt = JwtUtils.generateJwt(claims); //jwt包含了当前登录的员工信息
        return Result.success(200,"您已成功登录",jwt);

    }

    @Override
    public Result getUserInfo(String token) {
        Claims claims = JwtUtils.parseJWT(token);
        String phone = claims.get("phone", String.class);
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getPhone, phone);
        User user = getOne(lambdaQueryWrapper);
        if(user==null){
            return Result.fail("用户不存在！");
        }
        UserDto userDto = new UserDto();
        userDto.setPhone(user.getPhone());
        userDto.setName(user.getName());
        return Result.success(userDto);
    }

    @Override
    public Result uploadRedBookId(String token,String redBook_id) {
        Claims claims = JwtUtils.parseJWT(token);
        String phone = claims.get("phone", String.class);
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getPhone,phone);
        User one = getOne(lambdaQueryWrapper);
        one.setRedBook_id(redBook_id);
        userMapper.updateRedBook(phone,redBook_id);
        return Result.success();
    }
}




